package cn.bookingsmart.cfg;

import cn.bookingsmart.common.config.SystemConfig;
import cn.bookingsmart.service.EsSyncTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.StringUtils;

/**
 * Created by Tim.Yao
 * on 2017/3/27.
 *
 * @author: yaoting@digitalchina.com
 * @version: 1.0
 */
public class InstantiationTracingBeanPostProcessor implements ApplicationListener<ContextRefreshedEvent> {

    private static Logger log = LoggerFactory.getLogger(InstantiationTracingBeanPostProcessor.class);

    @Autowired
    private EsSyncTask esSyncTask;

    private static final String THREAD_COUNT = SystemConfig.get("es.sync.thread.count");

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
//            if (StringUtils.hasLength(THREAD_COUNT)) {
//                System.out.println("Event start ...");
//                int threadCount = 1;
//                try {
//                    threadCount = Integer.parseInt(THREAD_COUNT);
//                } catch (Exception e) {
//                    log.error("Wrong thread count configuration \"s%\", use default 1 thread to sync.", THREAD_COUNT);
//                }
//                System.out.println("Elasticsearch sync thread is starting, current thread count : " + threadCount);
//                esSyncTask.start(threadCount);
//                System.out.println("Event end");
//            }
        }
    }
}
